/**
 * $Id: BookServiceImpl.java 23 2011-06-15 13:58:35Z feigme $
 */
package com.iary.domain.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.iary.domain.model.book.Book;
import com.iary.domain.model.book.BookExistedException;
import com.iary.domain.model.book.BookRepository;
import com.iary.domain.service.BookService;
import com.iary.infrastructure.orm.Page;

@Component
public class BookServiceImpl implements BookService {

	private BookRepository bookRepository;

	BookServiceImpl() {

	}

	@Autowired
	public BookServiceImpl(BookRepository bookRepository) {
		this.bookRepository = bookRepository;
	}

	@Override
	public Long saveBook(Book book) throws BookExistedException {
		Book bookInDb = bookRepository.findBookByName(book.getName());
		if (bookInDb != null) {
			throw new BookExistedException("Existed book name : "
					+ book.getName());
		}
		bookRepository.save(book);
		return book.getId();
	}

	@Override
	public Page<Book> findAll(Page<Book> page) {
		return bookRepository.getAll(page);
	}

}
